import json
from io import BytesIO
from CAAURLLoader import CAAURLLoader


# 封装了url调用client，保留客户端一些数据
class CAAi3dxClient:
    ENCODING = "UTF-8"  # default encoding

    _URL_loader = None  # unique storage of CAAURLLoader instance

    def __init__(self, space3ds_url, pass3ds_url, output_dir):
        self._space3ds_url = space3ds_url  # 3DSpace Server URL
        self._pass3ds_url = pass3ds_url  # 3DPassport Server URL
        self._output_dir = output_dir  # output directory for traces

        # Try initializing the URL loader (assuming CAAURLLoader is a class you have or will create)
        try:
            self._URL_loader = CAAURLLoader()  # You need to define or import CAAURLLoader
        except Exception as e:
            print(f"Error initializing CAAURLLoader: {str(e)}")

        self._context = None

    def get_url_loader(self):
        return self._URL_loader

    def get_3d_space_server(self):
        return self._space3ds_url

    def get_passport_server(self):
        return self._pass3ds_url

    def get_out(self):
        return self._output_dir

    @staticmethod
    def get_json(byte_array):
        # Convert byte array to JSON object
        byte_stream = BytesIO(byte_array)
        json_obj = json.load(byte_stream)
        return json_obj
